namespace CAOS.Context.Migrations
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Linq;
    using CAOS.Entity;
    using System.Collections.Generic;

    internal sealed class Configuration : DbMigrationsConfiguration<CAOS.Context.CAOSContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = true;
            AutomaticMigrationDataLossAllowed = true;
        }

        protected override void Seed(CAOS.Context.CAOSContext context)
        {
            //  This method will be called after migrating to the latest version.

            //  You can use the DbSet<T>.AddOrUpdate() helper extension method 
            //  to avoid creating duplicate seed data. E.g.
            //
            //    context.People.AddOrUpdate(
            //      p => p.FullName,
            //      new Person { FullName = "Andrew Peters" },
            //      new Person { FullName = "Brice Lambson" },
            //      new Person { FullName = "Rowan Miller" }
            //    );
            //

            context.Database.ExecuteSqlCommand("delete from PAPEL_USUARIO");
            context.Database.ExecuteSqlCommand("delete from PAPEL_RECURSO");
            context.Database.ExecuteSqlCommand("delete from PAPEL");
            context.Database.ExecuteSqlCommand("delete from USUARIO");
            context.Database.ExecuteSqlCommand("delete from SISTEMA");
            context.Database.ExecuteSqlCommand("delete from RECURSO");


            Sistema sistema = new Sistema() { Sigla = "CAOS", Nome = "Controle de acesso", CodigoAutenticacao = "1234" };
            context.Sistemas.Add(sistema);

            Usuario usuario = new Usuario() { Nome = "Felipe", Login = "Felipe", Senha = "1234", Email = "felipe@felipe.com", Papeis = new List<Papel>(), IsAdministradorCAOS = true, CPF = "057378" };
            context.Usuarios.Add(usuario);

            Papel papel = new Papel() { Codigo = "ADMIN", Sistema = sistema, IsPapelGestor = true, Nome = "Administrador" };
            context.Papeis.Add(papel);

            usuario.Papeis.Add(papel);

            context.SaveChanges();

            base.Seed(context);
        }
    }
}
